package com.zhiqingchun.web.article;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhiqingchun.dao.article.ArticleResult;
import com.zhiqingchun.dao.article.IArticleDao;

@Controller
@RequestMapping("/conllectArticle.html")
public class CollectArticleController {

	@RequestMapping(method = RequestMethod.POST)
	public String collectArticle(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		try {
			PrintWriter out = response.getWriter();
			int id = Integer.parseInt(request.getParameter("id"));
			String _userId = request.getParameter("userId");
			if (_userId.isEmpty()) {
				out.print(0);
			} else {
				int userId = Integer.parseInt(_userId);
				ArticleResult articleResult = articleDao.collect(id, userId);
				out.print(articleResult.getCollectStatus());
			}
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String collectArticle2(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		return collectArticle(map, request, response);
	}
	
	@Autowired
	private IArticleDao articleDao;

	public IArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(IArticleDao articleDao) {
		this.articleDao = articleDao;
	}
}
